import math

class SCL_C90:
    def __init__(self, answers):
        self.answers = answers
        self.domain_scores = {
            'somatization': [],
            'obsessive_compulsive': [],
            'interpersonal_sensitivity': [],
            'depression': [],
            'anxiety': [],
            'hostility': [],
            'terror': [],
            'paranoia': [],
            'psychosis': [],
            'others': [],
        }

        self.type_sum = 90
        self.domain_scores_sum: int | None
        self.domain_scores_ave: int | None
        self.positive_num: int | None
        self.negative: int | None
        '''
        # domain_scores_sum: int | None #总分
        # domain_scores_ave: int | None #总均分
        # positive_num: int | None  #阳性项目数
        # negative: int | None #阴性项目数
        # positive_symptom_ave: False #阳性症状均分
        '''


    def _assign_answers_to_domains(self):
        # 躯体化、 强迫症状 、人际关系敏感、抑郁、焦虑、敌对、恐怖、偏执和精神病性
        # somatization obsessive_compulsive interpersonal_sensitivity, depression, anxiety, hostility, terror, paranoia and psychosis
        # 根据条目分配答案到各个领域
        self.domain_scores['somatization'] = [int(self.answers[f'q{i}']) for i in
                                              [1, 4, 12, 27, 40, 42, 48, 49, 52, 53, 56, 58]]
        self.domain_scores['obsessive_compulsive'] = [int(self.answers[f'q{i}']) for i in
                                                      [3, 9, 10, 28, 38, 45, 46, 51, 55, 65]]
        self.domain_scores['interpersonal_sensitivity'] = [int(self.answers[f'q{i}']) for i in
                                                           [6, 21, 34, 36, 37, 41, 61, 69, 73]]
        self.domain_scores['depression'] = [int(self.answers[f'q{i}']) for i in
                                            [5, 14, 15, 20, 22, 26, 29, 30, 31, 32, 54, 71, 79]]
        self.domain_scores['anxiety'] = [int(self.answers[f'q{i}']) for i in [2, 17, 23, 33, 39, 57, 72, 78, 80, 86]]
        self.domain_scores['hostility'] = [int(self.answers[f'q{i}']) for i in [11, 24, 63, 67, 74, 81]]
        self.domain_scores['terror'] = [int(self.answers[f'q{i}']) for i in [13, 25, 47, 50, 70, 75, 82]]
        self.domain_scores['paranoia'] = [int(self.answers[f'q{i}']) for i in [8, 18, 43, 68, 76, 83]]
        self.domain_scores['psychosis'] = [int(self.answers[f'q{i}']) for i in [7, 16, 35, 62, 77, 84, 85, 87, 88, 90]]
        self.domain_scores['others'] = [int(self.answers[f'q{i}']) for i in [19, 44, 59, 60, 64, 66, 89]]

    def _domain_scores_sum(self):
        count = 0
        for k, v in self.domain_scores.items():
            count += sum(v)
        return count

    def _domain_scores_ave(self):
        count = self._domain_scores_sum()
        if count is not None and count != 0:
            return math.floor(count / self.type_sum)
        return None

    def _positive_num(self):
        count = 0
        for i in range(1, 91):
            if int(self.answers[f'q{i}']) >= 2:
                count = count + 1
        return count

    def _negative_num(self):
        count = 0
        for i in range(1, 91):
            if int(self.answers[f'q{i}']) == 1:
                count = count + 1
        return count

    def _positive_symptom_ave(self):
        count_socres = self._domain_scores_sum()
        count_positive = self._positive_num()
        count_negative = self._negative_num()
        # return math.floor((count_socres - count_negative) / count_positive)
        if count_positive==0:
            return 0
        if count_positive!=0:
            return math.floor((count_socres - count_negative)/count_positive)

    def is_positive_symptom(self):
        count_socres = self._domain_scores_sum()
        count_positive = self._positive_num()

        if count_socres > 160:
            return True
        if count_positive > 43:
            return True

        domain_scores = self.domain_scores
        for k, v in domain_scores.items():
            if (math.floor(sum(v) / self.type_sum)) > 2:
                return True
        return False
        if count_socres > 160:
            return True
        if count_positive > 43:
            return True


    def sum_socre_and_grade(self):
        type_socre = {}
        for k, v in self.domain_scores.items():
            grade = []
            single_score = sum(v)
            single_grade = math.floor(sum(v) / len(v))
            grade.extend([single_score, single_grade])
            type_socre[k] = grade
        return type_socre
    def get_scores(self):
        self._assign_answers_to_domains()

        self._domain_scores_sum()
        self._domain_scores_ave()
        self._positive_num()
        self._negative_num()
        self._positive_symptom_ave()
        self.is_positive_symptom()

        type_socre = self.sum_socre_and_grade()

        all_type = {  "allscore": self._domain_scores_sum(),
                "allscorejun":  self._domain_scores_ave(),
                "sunproject": self._positive_num(),
                "moonproject": self._negative_num(),
                "yxzzjf": self._positive_symptom_ave(),
                "ifsun": self.is_positive_symptom(),
                "qth":type_socre["somatization"][0],
                "qthif": type_socre["somatization"][1],
                "qpz":type_socre["obsessive_compulsive"][0],
                "qpzif": type_socre["obsessive_compulsive"][1],
                "rjgxmg":type_socre["interpersonal_sensitivity"][0],
                "rjgxmgif": type_socre["interpersonal_sensitivity"][1],
                "yy":type_socre["depression"][0],
                "yyif": type_socre["depression"][1],
                "jl": type_socre["anxiety"][0],
                "jlif": type_socre["anxiety"][1],
                "dd": type_socre["hostility"][0],
                "ddid": type_socre["hostility"][1],
                "kb": type_socre["terror"][0],
                "kbif": type_socre["terror"][1],
                "pz": type_socre["paranoia"][0],
                "pzif": type_socre["paranoia"][1],
                "jsbx": type_socre["psychosis"][0],
                "jsbxif": type_socre["psychosis"][1],
                "qita": type_socre["others"][0],
                "qitaif": type_socre["others"][1],
            }
        return all_type

'''

{"allscore":"总分","allscorejun":"总均分","sunproject":"阳性项目数",
"moonproject":"阴性项目数","yxzzjf":"阳性症状均分","ifsun":"是否为阳性",
"qth":"躯体化分数","qpz":"强迫症状","rjgxmg":"人际关系敏感",
"yy":"抑郁","jl":"焦虑","dd":"敌对","kb":"恐怖","pz":"偏执","jsbx":"精神病性","qita":"其他"}

'''

# # 示例使用
# """
# 初始化：SCL_C90 类接收一个包含用户回答的字典 answers。
# 分配答案到各个领域：_assign_answers_to_domains 方法将每个条目的答案分配到相应的领域，根据您提供的条目号。
# 获取得分：get_scores 方法返回得分。
#
# 示例使用：提供一个示例回答字典，
# 通过这种方式，您可以在后端计算SCL-90的评分，。
# """
#
# answers = {
#     "q1": "3",
#     "q2": "2",
#     "q3": "1",
#     "q4": "5",
#     "q5": "2",
#     "q6": "1",
#     "q7": "3",
#     "q8": "2",
#     "q9": "1",
#     "q10": "5",
#     "q11": "3",
#     "q12": "2",
#     "q13": "1",
#     "q14": "5",
#     "q15": "2",
#     "q16": "1",
#     "q17": "3",
#     "q18": "2",
#     "q19": "1",
#     "q20": "5",
#     "q21": "3",
#     "q22": "2",
#     "q23": "1",
#     "q24": "5",
#     "q25": "2",
#     "q26": "1",
#     "q27": "3",
#     "q28": "2",
#     "q29": "1",
#     "q30": "5",
#     "q31": "3",
#     "q32": "2",
#     "q33": "1",
#     "q34": "5",
#     "q35": "2",
#     "q36": "1",
#     "q37": "3",
#     "q38": "2",
#     "q39": "1",
#     "q40": "5",
#     "q41": "3",
#     "q42": "2",
#     "q43": "1",
#     "q44": "5",
#     "q45": "2",
#     "q46": "1",
#     "q47": "3",
#     "q48": "2",
#     "q49": "1",
#     "q50": "5",
#     "q51": "3",
#     "q52": "2",
#     "q53": "1",
#     "q54": "5",
#     "q55": "2",
#     "q56": "1",
#     "q57": "3",
#     "q58": "2",
#     "q59": "1",
#     "q60": "5",
#     "q61": "3",
#     "q62": "2",
#     "q63": "1",
#     "q64": "5",
#     "q65": "2",
#     "q66": "1",
#     "q67": "3",
#     "q68": "2",
#     "q69": "1",
#     "q70": "5",
#     "q71": "3",
#     "q72": "2",
#     "q73": "1",
#     "q74": "5",
#     "q75": "2",
#     "q76": "1",
#     "q77": "3",
#     "q78": "2",
#     "q79": "1",
#     "q80": "5",
#     "q81": "3",
#     "q82": "2",
#     "q83": "1",
#     "q84": "5",
#     "q85": "2",
#     "q86": "1",
#     "q87": "3",
#     "q88": "2",
#     "q89": "1",
#     "q90": "5"
# }
#
# scl_c90 = SCL_C90(answers)
# all_type = scl_c90.get_scores()
# print(all_type)
#
#
